package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.common.PageParam;
import com.example.park_management.common.Result;
import com.example.park_management.domain.*;
import com.example.park_management.domain.vo.LoginResultVO;
import com.example.park_management.service.EmployeesService;
import com.example.park_management.service.NoticesService;
import com.example.park_management.util.JwtUtil;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/employees")
public class EmployeesController {


    @Value("${config.shiro.secret}")
    private String secret;

    @Resource
    EmployeesService employeesService;

    @Resource
    NoticesService noticesService;
    @PostMapping("/login")
    Result employeesLogin(@RequestBody Employees employees) {
        Employees one = employeesService.getOne(new QueryWrapper<Employees>().eq("username", employees.getUsername()).eq("password", employees.getPassword()));
        if (one != null) {
            LoginResultVO loginResultVO = new LoginResultVO();
            loginResultVO.setUsername(one.getUsername());
            loginResultVO.setUsername(one.getPassword());
            loginResultVO.setToken(JwtUtil.sign(one.getEmployeeId().toString(), secret, "employees"));
            return Result.success(loginResultVO);
        }
        return Result.fail("账号或密码错误");
    }


    @PostMapping("/register")
    Result register(@RequestBody Employees employees) {
        if (StringUtils.isBlank(employees.getUsername()) || StringUtils.isBlank(employees.getPassword()) || StringUtils.isBlank(employees.getIdNumber())) {
            return Result.fail("必填属性为空");
        }
        Employees one = employeesService.getOne(new QueryWrapper<Employees>().eq("username", employees.getUsername()));
        if (one != null) {
            return Result.fail("用户名已存在");
        }
        employeesService.save(employees);
        return Result.success();
    }

    @DeleteMapping("/{employeeId}")
    public Result delete(@PathVariable Integer employeeId) {
        //检验楼栋下面是否有楼层，若有提示不允许删除楼栋应先删除楼层数据
        LambdaQueryWrapper<Notices> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notices::getNoticeId,employeeId);
        List<Notices> list = noticesService.list(queryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            String fStr = list.stream().map(f -> f.getNoticeId().toString()).collect(Collectors.joining());
            return Result.fail("删除失败:"+fStr);
        }
        boolean b = noticesService.removeById(employeeId);
        if (b){
            return Result.success();
        }
        return Result.fail();
    }

    @PutMapping("/")
    public Result update(@RequestBody Employees employees) {
        try{
            if (StringUtils.isBlank(employees.getUsername()) || StringUtils.isBlank(employees.getPassword()) || StringUtils.isBlank(employees.getIdNumber())) {
                return Result.fail("必填属性为空");
            }
            boolean b = employeesService.updateById(employees);
            if (b) {
                return Result.success();
            }
            return Result.fail();
        }catch (Exception e) {
            return Result.fail("输入格式异常！");
        }
    }

    @PostMapping("/page")
    public Result page(@RequestBody PageParam<Employees> pageParam) {
        Page<Employees> employeesPage = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());

        QueryWrapper<Employees> employeesQueryWrapper = new QueryWrapper<>();
        if (pageParam.getData() != null) {
            if (pageParam.getData().getUsername() != null) {
                employeesQueryWrapper.like("username", pageParam.getData().getUsername());
            }
            if (pageParam.getData().getRealName() != null) {
                employeesQueryWrapper.like("real_name", pageParam.getData().getRealName());
            }
            if (pageParam.getData().getPhoneNumber() != null) {
                employeesQueryWrapper.like("phone_number", pageParam.getData().getPhoneNumber());
            }
        }
        employeesService.page(employeesPage, employeesQueryWrapper);
        return Result.success(employeesPage.getRecords(), employeesPage.getTotal());
    }


}
